function [ result ] = promptZAP( varargin )
%PROMPTZAP Prompts user to select traces and frequency bounds for ZAP
%analysis, runs the ZAP and prints .m-file format results
%
% If an argument is specified, it should be a string specifying the
% filename of a single ZAP response trace (e.g. chirp, PRBS). If none is
% specified, this functions looks for a "currentTrace" variable in the
% workspace and uses its userFcn.filename field, if present - this means it
% works as a qvTrace user function.
%
% Ben Suter 2009-10-12
% Ben Suter 2009-11-17 Added support for new numBoxcarSamples argument, and
% included this parameter in the .m-file format output. If this parameter
% is >1, the data will be smoothed with a boxcar sliding window. Also
% changed the output argument from zap (unused) to result.
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

minFreq = 0.5;
maxFreq = 20;
numBoxcarSamples = 1;

if nargin > 0 && (exist(varargin{1}, 'file') == 2)
    filename = varargin{1};
else
    filename = evalin('base','currentTrace.userFcn.filename');
end

[pathstr, name, ext] = fileparts(filename);

% File selection dialog adapted from selectFilesFromList.m
d = dir(fullfile(pathstr, ['/*' ext]));
fileList = sortrows({d.name}');
initialIndex = strmatch([name ext], fileList, 'exact');
[selection, ok]  = listdlg('PromptString', 'Select ZAP traces', 'OKString', 'Run ZAP', 'SelectionMode', 'multiple', 'ListString', fileList, 'Name', 'Select ZAP traces', 'InitialValue', initialIndex);

if ok
    if length(selection) < 1
        disp('promptZAP: no traces were selected');
        return;
    end
    
    % Build the array of trace numbers, b/c that's what ZAP expects
    for i = 1:length(selection)
        fn = fileList(selection(i));
        fn = fn{:}; % convert from 1x1 cell array to fixed-length string
        sto = length(fn)-length(ext);
        sta = sto - 3;
        traceNums(i) = str2num(fn(sta:sto));
    end
    prefix = [ pathstr '/' fn(1:sta-1) ];
    
    answer = inputdlg({'Min frequency', 'Max frequency', '# of samples to smooth'}, 'Bounds on frequency search, and smoothing?', 1, {num2str(minFreq), num2str(maxFreq), num2str(numBoxcarSamples)});
    if ~ isempty(answer)
        minFreq = str2double(answer{1});
        maxFreq = str2double(answer{2});
        numBoxcarSamples = str2double(answer{3});
    end

    % Run the ZAP analysis
    [ resonantFreq ratio freq idx resp stim respf stimf ] = ZAP( prefix, traceNums, minFreq, maxFreq, numBoxcarSamples );
    result.resonantFreq = resonantFreq;
    result.minFreq = minFreq;
    result.maxFreq = maxFreq;
    result.numBoxcarSamples = numBoxcarSamples;
    result.traceNums = traceNums;
    
    % Print the results in format for copy-paste into .m-file
    outPrefix = 'map.ZAP.';
    disp('% ********** ZAP: sub-threshold frequency response **********')
    fnames = fieldnames(result);
    for n = 1 : size(fnames,1)
        if (max(size(result.(fnames{n})))) == 1
            disp([outPrefix fnames{n} ' = ' num2str(result.(fnames{n})) ';']);
        else
            disp([outPrefix fnames{n} ' = [' num2str(result.(fnames{n})) '];']);
        end
    end

end
